package com.ss.timetip;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException;
import org.jnativehook.keyboard.NativeKeyEvent;
import org.jnativehook.keyboard.NativeKeyListener;

import java.util.logging.Level;
import java.util.logging.Logger;

public class Main extends Application {

    private static final Logger logger = Logger.getLogger(GlobalScreen.class.getPackage().getName());

    @Override
    public void start(Stage primaryStage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("/ui/sample.fxml"));
        primaryStage.setTitle("time tip");
        Scene sence = new Scene(root, 300, 275);
        primaryStage.setScene(sence);
        primaryStage.show();

        Platform.setImplicitExit(false);

        HotKey hotKey = new HotKey();
        hotKey.setKey(() -> {
            if (primaryStage.isShowing()) {
                Platform.runLater(primaryStage::close);
            } else {
                Platform.runLater(primaryStage::show);
            }
        }, NativeKeyEvent.VC_1, NativeKeyEvent.VC_CONTROL);

    }


    private static class HotKey {
        int[] markKy;
        int masterKey;
        int markNum = 0;
        Runnable run;


        public HotKey() {
            logger.setLevel(Level.OFF);
            try {
                GlobalScreen.registerNativeHook();
            } catch (NativeHookException ex) {
                ex.printStackTrace();
            }
            GlobalScreen.addNativeKeyListener(new NativeKeyListener() {
                @Override
                public void nativeKeyTyped(NativeKeyEvent nativeKeyEvent) {

                }

                @Override
                public void nativeKeyPressed(NativeKeyEvent nativeKeyEvent) {
                    press(nativeKeyEvent.getKeyCode());
                }

                @Override
                public void nativeKeyReleased(NativeKeyEvent nativeKeyEvent) {
                    release(nativeKeyEvent.getKeyCode());
                }
            });
        }

        public void setKey(Runnable run, int masterKey, int... markKy) {
            this.markKy = markKy;
            this.masterKey = masterKey;
            this.run = run;
        }

        public void press(int keyCode) {
            if (inKey(keyCode)) {
                markNum++;
            }
            if ((markKy == null || markNum == markKy.length) && keyCode == masterKey && run != null) {
                run.run();
            }
        }

        public void release(int keyCode) {
            if (inKey(keyCode)) {
                markNum--;
            }
        }

        public boolean inKey(int keyCode) {
            for (int k : markKy) {
                if (k == keyCode) {
                    return true;
                }
            }
            return false;
        }

    }


}
